package com.gameLogic.scene
{
	import com.gameLogic.utils.GameConfig;
	import com.gameLogic.utils.ObjDictionary;
	
	import laya.display.Sprite;
	import laya.maths.Point;
	import laya.maths.Rectangle;
	
	public class WorldMap
	{
		
		private var _container:Sprite;
		public function get container():Sprite
		{
			return _container;
		}
		
		private var _gridVO:MapGridVO = new MapGridVO();
		public function get gridVO():MapGridVO
		{
			return _gridVO;
		}
		
		private var _gridTiles:Vector.<Vector.<MapTile>>;
		public function get gridTiles():Vector.<Vector.<MapTile>>
		{
			return _gridTiles;
		}
		
		/**
		 * 已经加载的Tile 
		 */		
		private var _loadedTiles:ObjDictionary = new ObjDictionary();
		
		public function WorldMap()
		{
			_container = new Sprite();
			init();
		}
		
		public function init():void
		{
			//var bg:Sprite = new Sprite();
			//bg.loadImage("../../res/bg3.jpg");
			//_container.addChild(bg);
			createTiles();
		}
		
		private function createTiles():void
		{
			_gridTiles = new Vector.<Vector.<MapTile>>();
			for(var col:uint=0; col<_gridVO.col; col++)
			{
				_gridTiles[col] = new Vector.<MapTile>();
				for(var row:uint=0; row<_gridVO.row; row++)			
				{
					_gridTiles[col][row] = new MapTile(row, col, _container);
				}
			}
		}
		
		private function loadAllTiles():void
		{
			for(var col:uint=0; col<_gridVO.col; col++)
			{
				for(var row:uint=0; row<_gridVO.row; row++)			
				{
					_gridTiles[col][row].loadTile();
				}
			}
		}
		
		public function update():void
		{
			var camView:Rectangle = SceneManager.instance.camera.cameraView; 
			var tiles:ObjDictionary = getNeedLoadTiles(camView.x, camView.y);
			loadTiles(tiles);
		}
		
		public function loadTiles(tiles:ObjDictionary):void
		{
			if(tiles && tiles.length > 0)
			{
				var col:int, row:int;
				var arr:Array, key:String;
				var strKey:String;
				//先卸载
				for(key in _loadedTiles.container)
				{
					if(!tiles.containsKey(key))
					{
						arr = key.split("_");
						col = parseInt(arr[0]);
						row = parseInt(arr[1]);
						_gridTiles[col][row].unloadTile();
						strKey = col + "_" + row;
						_loadedTiles.remove(strKey);
					}
				}
				//再加载
				for(key in tiles.container)
				{
					if(!_loadedTiles.containsKey(key))
					{
						arr = key.split("_");
						col = parseInt(arr[0]);
						row = parseInt(arr[1]);
						if(col < _gridVO.col && row < _gridVO.row)
						{
							_gridTiles[col][row].loadTile();
							strKey = col + "_" + row;
							_loadedTiles.add(strKey, strKey);
						}
					}
				}
			}
		}
		
		public function getNeedLoadTiles(camX:Number, camY:Number):ObjDictionary
		{
			var cellW:int = gridVO.cellW;
			var cellH:int = gridVO.cellH;
			//var rect:Rectangle = new Rectangle(camX-cellW*0.5, camY-cellH*0.5, GameConfig.DeviceW + cellW, GameConfig.DeviceH + cellH);
			var rect:Rectangle = new Rectangle(camX-cellW, camY-cellH, GameConfig.DeviceW + cellW*2, GameConfig.DeviceH + cellH*2);
			var p1:Point = scenePosToGrid(rect.x, rect.y);
			var p2:Point = scenePosToGrid(rect.right, rect.bottom);
			
			var res:ObjDictionary = new ObjDictionary();
			var centerP:Point;
			var key:String;
			for(var i:int=p1.x; i<=p2.x; i++)
			{
				for(var j:int=p1.y; j<=p2.y; j++)
				{
					centerP = gridToScenePos(i,j);
					if(rect.contains(centerP.x, centerP.y))
					{
						key = i + "_" + j;
						res.add(key, key);
					}
				}
			}
			return res;
		}
		
		public function scenePosToGrid(x:Number, y:Number):Point
		{
			var p:Point = new Point();
			p.x = parseInt(x / _gridVO.cellW + "");     //列
			p.y = parseInt(y / _gridVO.cellH + "");     //行
			return p;
		}
		
		public function gridToScenePos(x:int, y:int):Point
		{
			var p:Point = new Point();
			p.x = (x +0.5) *  _gridVO.cellW;     //列
			p.y = (y + 0.5) * _gridVO.cellH;     //行
			return p;
		}
		
		private static var _instance:WorldMap
		public static function get instance():WorldMap
		{
			if(!_instance){ _instance = new WorldMap(); }
			return _instance;
		}
		
	}
}


